<!DOCTYPE html>
<html>

<head runat="server">
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport"
    content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no" />
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="mobile-web-app-capable" content="yes" />
  <title>ONLYOFFICE</title>
  <link rel="icon" href="images/<%# editor.documentType %>.ico" type="image/x-icon" />
  <link rel="stylesheet" type="text/css" href="public/stylesheets/editor.css" />
  <script>
    localStorage.setItem('search_params', new URLSearchParams(location.search).toString())
  </script>
</head>

<body class="content" style="margin: 0;">
  <!-- <div onclick="tap()">content</div> -->
  <div class="form">
    <div id="iframeEditor"></div>
  </div>
  <script type="text/javascript" src="<%= apiUrl %>"></script>
  <script type="text/javascript" language="javascript">

    var docEditor = {};
    var config = {};

    var configString = '<%- JSON.stringify(config) %>'
    config = JSON.parse(configString);
    // console.log(config)
    var onAppReady = () => {
      window.postMessage('message')
    }
    var onRequestSaveAs = (event) => {
      //
      console.log(event)
      debugger
    }

    config.events = {
      "onAppReady": onAppReady,
      // "onDocumentStateChange": onDocumentStateChange,
      // "onRequestEditRights": onRequestEditRights,
      // "onError": onError,
      // "onOutdatedVersion": onOutdatedVersion,
      // "onMakeActionLink": onMakeActionLink,
      // "onMetaChange": onMetaChange,
      // "onRequestInsertImage": onRequestInsertImage,
      // "onRequestCompareFile": onRequestCompareFile,
      // "onRequestMailMergeRecipients": onRequestMailMergeRecipients,
    };

    if (config != null) {
      // config.events.onRequestHistory = onRequestHistory;
      // config.events.onRequestHistoryData = onRequestHistoryData;
      // config.events.onRequestHistoryClose = onRequestHistoryClose;
      // config.events.onRequestRename = onRequestRename;
      // config.events.onRequestUsers = onRequestUsers;
      // config.events.onRequestSendNotify = onRequestSendNotify;
      // config.events.onRequestReferenceData = onRequestReferenceData;
      // config.events.onRequestSaveAs = onRequestSaveAs;
    }

    var connectEditor = function () {
      docEditor = new DocsAPI.DocEditor("iframeEditor", config);
      docEditor.serviceCommand("commond start");
      fixSize();
    };

    // get the editor sizes
    var fixSize = function () {
      var wrapEl = document.getElementsByClassName("form");
      if (wrapEl.length) {
        wrapEl[0].style.height = screen.availHeight + "px";
        window.scrollTo(0, -1);
        wrapEl[0].style.height = window.innerHeight + "px";
      }
    };

    if (window.addEventListener) {
      window.addEventListener("load", connectEditor);
      window.addEventListener("resize", fixSize);
    } else if (window.attachEvent) {
      window.attachEvent("onload", connectEditor);
      window.attachEvent("onresize", fixSize);
    }
  </script>
  <script>
    window.addEventListener('pluginMessage', function (event) {
      var message = event;
      // 处理插件发送的消息
      console.log('收到插件消息：', message.type);
    });
    window.addEventListener('onmessage', () => {
      console.log('onmessage', message.type);
    })
  </script>
</body>

</html>